package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.*;
import com.guigu.pojo.*;
import com.guigu.service.PurchaseDetailInfoService;
import com.guigu.service.SupplierSupplyOfGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class PurchaseDetailInfoServiceImpl extends
        ServiceImpl<PurchaseDetailInfoMapper, PurchaseDetailInfo>
implements PurchaseDetailInfoService {

    @Autowired
    PurchaseInfoMapper purchaseInfoMapper;//采购表
    @Autowired
    PurchaseDetailInfoMapper purchaseDetailMapper; //采购详情mapper
    @Autowired
    GatherInfoMapper gatherInfoMapper;//入库表
    @Autowired
    GatherDetailInfoMapper gatherDetailInfoMapper;//入库详情表
    @Autowired
    WarehouseInfoMapper warehouseInfoMapper;//仓库表
    @Autowired
    WarehouseDetailsInfoMapper warehouseDetailsInfoMapper;//仓库详情表
    @Autowired
    SupplierSupplyOfGoodsService SupplyService;//供应商维护商品表Service
    @Autowired
    SupplierSupplyOfGoodsMapper supplierSupplyOfGoodsMapper;//商品维护表

    @Override
    public Page<PurchaseDetailInfo> queryPurchaseDetails(PurchaseDetailInfo detail, Integer pageno, Integer pagesize) {
        QueryWrapper<PurchaseDetailInfo> queryWrapper=
                new QueryWrapper<PurchaseDetailInfo>();
        if (detail.getPid()!=null){
            queryWrapper.eq("pid",detail.getPid());
        }
        Page<PurchaseDetailInfo> page = this.page(new Page<PurchaseDetailInfo>(pageno, pagesize),queryWrapper);
        for (PurchaseDetailInfo pd : page.getRecords()) {
            //设置供应商维护表
            pd.setSupply(SupplyService.queryById(pd.getSupplyOrderId()));
        }
        return page;
    }

    //改变设计状态，并且指定仓库
    @Override
    public Map changeDetailInfoState(Integer purchaseDetailInfoId,Integer warId) {
        Map map=new HashMap<String,Object>();
        //根据采购详情id，获取对象
        PurchaseDetailInfo purchaseDetailInfo =purchaseDetailMapper.selectById(purchaseDetailInfoId);
        //设置为已设计
        purchaseDetailInfo.setIsDesign(1);
        //进行修改
        purchaseDetailMapper.updateById(purchaseDetailInfo);
        //获取采购表对象
        PurchaseInfo purchaseInfo=purchaseInfoMapper.selectById(purchaseDetailInfo.getPid());
        //获取入库申请表对象
        QueryWrapper queryWrapper=new QueryWrapper<GatherInfo>();
        //采购单
        queryWrapper.eq("tracking_number",purchaseInfo.getBuyNumber());
        //不查询审核未通过的数据
        queryWrapper.ne("is_check",2);
        GatherInfo gatherInfo=gatherInfoMapper.selectOne(queryWrapper);
        //获取入库申请表下的详情对象
        queryWrapper=new QueryWrapper<GatherDetailInfo>();
        queryWrapper.eq("gather_id",gatherInfo.getId());//父id
        queryWrapper.eq("supply_order_id",purchaseDetailInfo.getSupplyOrderId());//商品维护表id
        GatherDetailInfo gatherDetailInfo=gatherDetailInfoMapper.selectOne(queryWrapper);
        if(gatherDetailInfo!=null){
            //找到该入库详情表，指定仓库
            gatherDetailInfo.setWarId(warId);
            //进行修改
            gatherDetailInfoMapper.updateById(gatherDetailInfo);
            //获取入库详情的商品维护表id获取商品维护表对象
            SupplierSupplyOfGoods supplierSupplyOfGoods=supplierSupplyOfGoodsMapper.selectById(gatherDetailInfo.getSupplyOrderId());
            //获取仓库对象
            WarehouseInfo warehouseInfo=warehouseInfoMapper.selectById(warId);
            //获取仓库详情对象，进行条件筛选
            queryWrapper=new QueryWrapper<WarehouseDetailsInfo>();
            queryWrapper.eq("p_id",warehouseInfo.getId());
            queryWrapper.eq("shop_id",supplierSupplyOfGoods.getGId());
            WarehouseDetailsInfo warehouseDetailsInfo=warehouseDetailsInfoMapper.selectOne(queryWrapper);
            //根据指定的仓库下的仓库详情的锁定库存进行增加
            warehouseDetailsInfo.setLockStock(warehouseDetailsInfo.getLockStock()+gatherDetailInfo.getShopNum());
            //进行修改
            warehouseDetailsInfoMapper.updateById(warehouseDetailsInfo);
        }
        map.put("msg","成功指定到该仓库");
        map.put("x",true);
        return map;
    }

    //重新指定仓库，对仓库的锁定库存进行修正增加
    @Override
    public Map anewChangeDetailInfoState(Integer purchaseDetailInfoId, Integer warId) {
        Map map=new HashMap<String,Object>();
        //根据采购详情id，获取对象
        PurchaseDetailInfo purchaseDetailInfo =purchaseDetailMapper.selectById(purchaseDetailInfoId);
        //获取采购表对象
        PurchaseInfo purchaseInfo=purchaseInfoMapper.selectById(purchaseDetailInfo.getPid());
        //获取入库申请表对象
        QueryWrapper queryWrapper=new QueryWrapper<GatherInfo>();
        //采购单
        queryWrapper.eq("tracking_number",purchaseInfo.getBuyNumber());
        //不查询审核未通过的数据
        queryWrapper.ne("is_check",2);
        GatherInfo gatherInfo=gatherInfoMapper.selectOne(queryWrapper);
        //获取入库申请表下的详情对象
        queryWrapper=new QueryWrapper<GatherDetailInfo>();
        queryWrapper.eq("gather_id",gatherInfo.getId());//父id
        queryWrapper.eq("supply_order_id",purchaseDetailInfo.getSupplyOrderId());//商品维护表id
        GatherDetailInfo gatherDetailInfo=gatherDetailInfoMapper.selectOne(queryWrapper);
        if(gatherDetailInfo!=null){
            //找到该入库详情表
            //获取入库详情的商品维护表id获取商品维护表对象
            SupplierSupplyOfGoods supplierSupplyOfGoods=supplierSupplyOfGoodsMapper.selectById(gatherDetailInfo.getSupplyOrderId());
            //获取仓库对象
            WarehouseInfo warehouseInfo=warehouseInfoMapper.selectById(warId);
            //获取仓库详情对象，进行条件筛选
            queryWrapper=new QueryWrapper<WarehouseDetailsInfo>();
            queryWrapper.eq("p_id",warehouseInfo.getId());
            queryWrapper.eq("shop_id",supplierSupplyOfGoods.getGId());
            WarehouseDetailsInfo warehouseDetailsInfo=warehouseDetailsInfoMapper.selectOne(queryWrapper);
            //根据指定的仓库下的仓库详情的锁定库存进行减少
            warehouseDetailsInfo.setLockStock(warehouseDetailsInfo.getLockStock()-gatherDetailInfo.getShopNum());
            //进行修改
            warehouseDetailsInfoMapper.updateById(warehouseDetailsInfo);
            //重新指定仓库id
            gatherDetailInfo.setWarId(warId);
            //进行修改
            gatherDetailInfoMapper.updateById(gatherDetailInfo);
            //获取新指定的仓库，对新的仓库详情数量进行增加
            WarehouseInfo temp_warehouseInfo=warehouseInfoMapper.selectById(warId);
            queryWrapper=new QueryWrapper<WarehouseDetailsInfo>();
            queryWrapper.eq("p_id",temp_warehouseInfo.getId());
            queryWrapper.eq("shop_id",supplierSupplyOfGoods.getGId());
            WarehouseDetailsInfo temp_warehouseDetailsInfo=warehouseDetailsInfoMapper.selectOne(queryWrapper);
            temp_warehouseDetailsInfo.setLockStock(temp_warehouseDetailsInfo.getLockStock()+gatherDetailInfo.getShopNum());
            warehouseDetailsInfoMapper.updateById(temp_warehouseDetailsInfo);
        }
        map.put("msg","重新指定到该仓库");
        map.put("x",true);
        return map;
    }
}
